<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <span>鱼 OJ 登录</span>
        </div>
      </template>

      <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
          label-width="0"
          @keyup.enter="handleLogin"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              placeholder="用户名 / 邮箱"
              prefix-icon="User"
              size="large"
          />
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              prefix-icon="Lock"
              size="large"
              show-password
          />
        </el-form-item>

        <!-- 登录按钮 -->
        <el-form-item>
          <el-button
              type="primary"
              size="large"
              :loading="loading"
              style="width: 100%"
              @click="handleLogin"
          >
            登 录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="tips">
        <span>还没有账号？<a @click="$router.push('/user/register')">立即注册</a></span>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import {UserControllerService, type UserLoginRequest} from "../../../generated";
import {useUserStore} from "@/stores/user.ts";

const router = useRouter()
const loginFormRef = ref<FormInstance>()

const loginForm = reactive({
  username: '',
  password: '',
})

const rules: FormRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

const loading = ref(false)

const userStore = useUserStore()

const handleLogin = async () => {
  try {
    const valid = await loginFormRef.value?.validate()
    if (!valid) return

    loading.value = true

    const res = await UserControllerService.userLogin({
      userAccount: loginForm.username,
      userPassword: loginForm.password
    } as UserLoginRequest)
    console.log(res)
    if (res.code === 0) {
      userStore.login(res.data)
      localStorage.setItem('user', JSON.stringify(res.data))
      await router.push('/')
      ElMessage.success("登录成功!")
    } else {
      ElMessage.error(res.message)
    }
  } catch (err) {
    console.error(err)
    ElMessage.error("登录失败，请稍后重试")
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #74ebd5, #9face6);
}

.login-card {
  width: 400px;
  max-width: 90%;
}

.card-header {
  text-align: center;
  font-size: 18px;
  font-weight: bold;
}

.tips {
  margin-top: 12px;
  text-align: right;
  font-size: 13px;
  color: #666;
}
</style>